<template>
  <div>
    <el-dialog title="编辑" :visible.sync="dialogVisibleEditType" width="30%">
      <div>
        <el-form
          ref="EditForm"
          :model="formEdit"
          :rules="rulesEdit"
          label-width="80px"
        >
          <el-form-item label="分类名称">
            <el-input
              v-model="formEdit.name"
              placeholder="请输入分类名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类权重">
            <el-input
              v-model="formEdit.weight"
              placeholder="请输入分类权重"
            ></el-input>
          </el-form-item>
          <el-form-item label="上级id">
            <el-cascader
              v-model="formEdit.parent_id"
              :options="fatherOptions"
              :props="{
                label: 'name',
                value: 'id',
                checkStrictly: true,
                children: 'children',
                expandTrigger: 'click',
              }"
              @change="changeFather"
            ></el-cascader>
          </el-form-item>

          <el-form-item label="封面图">
            <el-upload
              style="margin-right: 30px"
              class="upload-demo"
              :action="uploadCoverImgUrl"
              :on-success="onSuccessFileCover"
              :on-error="onErrorFileCover"
              :on-remove="onRemoveFileCover"
              :data="{ type: '13' }"
              list-type="text"
            >
              <el-button size="small" type="primary">上传封面图</el-button>
            </el-upload>

            <div v-if="formEdit.cover_img">
              <img
                :src="formEdit.cover_img"
                alt=""
                style="width: 120px; height: 120px"
              />
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogEdit">取 消</el-button>
        <el-button type="primary" @click="sureDialogEdit('EditForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["editTypeDetails", "tableDataRecharge"],
  data() {
    return {
      dialogVisibleEditType: false,
      formEdit: {
        name: "",
        parent_id: "",
        cover_img: "",
        weight: "",
      },
      fatherOptions: [],
      rulesEdit: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      uploadCoverImgUrl: process.env.VUE_APP_BASE_API + "/app/uploadFiles",
    };
  },
  watch: {
    editTypeDetails: {
      handler(newVal) {
        this.formEdit = newVal;
      },
    },
    tableDataRecharge: {
      handler(newVal) {
        this.fatherOptions = newVal;
      },
    },
  },
  methods: {
    changeFather(val) {
      console.log(val);
      this.formEdit.parent_id = val[val.length - 1];
      console.log(this.formEdit);
    },

    // 上传主图
    onSuccessFileCover(val) {
      this.formEdit.cover_img = val.data.path;
    },
    onErrorFileCover(val) {},

    onRemoveFileCover(val) {
      this.formEdit.cover_img = "";
    },

    // 确定
    sureDialogEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("sureDialogEdit", this.formEdit);
        } else {
          return false;
        }
      });
    },

    // 取消
    cancelDialogEdit() {
      this.$emit("cancelDialogEdit", "");
    },
  },
};
</script>
<style scoped lang="scss"></style>
